/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.TX;

import entity.Advertisement;
import entity.AdvertisementStatistics;
import entity.Country;
import entity.FoodItem;
import entity.Outlet;
import entity.StakeholderFood;
import entity.Users;
import entity.Places;
import entity.Reviews;
import entity.StakeholderTier;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Chen Zhe
 */
@Stateless
public class PopulateTTAneededDataSB implements PopulateTTAneededDataSBRemote {

    @PersistenceContext()
    EntityManager em;

    public void populate() {

        //create Jacob@gmail.com
        Users customer1 = new Users();
        customer1.setId(new Long(10001));
        Query queryChn = em.createQuery("SELECT c FROM Country c WHERE c.country= :china");
        queryChn.setParameter("china", "CHINA");
        List<Country> chnList = queryChn.getResultList();
        Country chn = chnList.get(0);
        customer1.setCountry(chn);
        Calendar birthDay = Calendar.getInstance();
        birthDay.set(Calendar.YEAR, 1986);
        customer1.setBirthDate(birthDay.getTime());
        customer1.setEmail("Jacob@gmail.com");
        customer1.setLoyaltypoint(new Integer(0));
        em.persist(customer1);
        em.flush();

        //create Randall@gmail.com
        Users customer2 = new Users();
        customer2.setId(new Long(10002));
        Query queryHk = em.createQuery("SELECT c FROM Country c WHERE c.country= :hk");
        queryHk.setParameter("hk", "HONG KONG SAR");
        List<Country> hkList = queryHk.getResultList();
        Country hk = hkList.get(0);
        customer2.setCountry(hk);
        Calendar birthDay2 = Calendar.getInstance();
        birthDay2.set(Calendar.YEAR, 1976);
        customer2.setBirthDate(birthDay2.getTime());
        customer2.setEmail("Randall@gmail.com");
        customer2.setLoyaltypoint(new Integer(0));
        em.persist(customer2);
        em.flush();

        //create Weida@gamil.com
        Users customer3 = new Users();
        customer3.setId(new Long(10003));
        Query queryMalaysia = em.createQuery("SELECT c FROM Country c WHERE c.country= :malaysia");
        queryMalaysia.setParameter("malaysia", "MALAYSIA");
        List<Country> mList = queryMalaysia.getResultList();
        Country m = mList.get(0);
        customer3.setCountry(m);
        Calendar birthDay3 = Calendar.getInstance();
        birthDay3.set(Calendar.YEAR, 1966);
        customer3.setBirthDate(birthDay3.getTime());
        customer3.setEmail("Weida@gmail.com");
        customer3.setLoyaltypoint(new Integer(0));
        em.persist(customer3);
        em.flush();

        //create Mingzhou@gmail.com
        Users customer4 = new Users();
        customer4.setId(new Long(10004));
        Query queryIndia = em.createQuery("SELECT c FROM Country c WHERE c.country= :india");
        queryIndia.setParameter("india", "INDIA");
        List<Country> iList = queryIndia.getResultList();
        Country india = iList.get(0);
        customer4.setCountry(india);
        Calendar birthDay4 = Calendar.getInstance();
        birthDay4.set(Calendar.YEAR, 1956);
        customer4.setBirthDate(birthDay4.getTime());
        customer4.setEmail("Mingzhou@gmail.com");
        customer4.setLoyaltypoint(new Integer(0));
        em.persist(customer4);
        em.flush();

        //create Stewart@gmail.com
        Users customer5 = new Users();
        customer5.setId(new Long(10005));
        Query queryThai = em.createQuery("SELECT c FROM Country c WHERE c.country= :thai");
        queryThai.setParameter("thai", "THAILAND");
        List<Country> thaiList = queryThai.getResultList();
        Country thai = thaiList.get(0);
        customer5.setCountry(thai);
        Calendar birthDay5 = Calendar.getInstance();
        birthDay5.set(Calendar.YEAR, 1946);
        customer5.setBirthDate(birthDay5.getTime());
        customer5.setEmail("Stewart@gmail.com");
        customer5.setLoyaltypoint(new Integer(0));
        em.persist(customer5);
        em.flush();

        //create Fenglong@gmail.com
        Users customer6 = new Users();
        customer6.setId(new Long(10006));
        Query queryJp = em.createQuery("SELECT c FROM Country c WHERE c.country= :jp");
        queryJp.setParameter("jp", "JAPAN");
        List<Country> jpList = queryJp.getResultList();
        Country japan = jpList.get(0);
        customer6.setCountry(japan);
        Calendar birthDay6 = Calendar.getInstance();
        birthDay6.set(Calendar.YEAR, 1996);
        customer6.setBirthDate(birthDay6.getTime());
        customer6.setEmail("Fenglong@gmail.com");
        customer6.setLoyaltypoint(new Integer(0));
        em.persist(customer6);
        em.flush();

        //create Congcong@gmail.com
        Users customer7 = new Users();
        customer7.setId(new Long(10007));
        Query querySg = em.createQuery("SELECT c FROM Country c WHERE c.country= :sg");
        querySg.setParameter("sg", "SINGAPORE");
        List<Country> sgList = querySg.getResultList();
        Country singapore = sgList.get(0);
        customer7.setCountry(singapore);
        Calendar birthDay7 = Calendar.getInstance();
        birthDay7.set(Calendar.YEAR, 1996);
        customer7.setBirthDate(birthDay7.getTime());
        customer7.setEmail("Congcong@gmail.com");
        customer7.setLoyaltypoint(new Integer(0));
        em.persist(customer7);
        em.flush();
        //create Eleanor@gmail.com
        Users customer8 = new Users();
        customer8.setId(new Long(10008));
        customer8.setCountry(singapore);
        Calendar birthDay8 = Calendar.getInstance();
        birthDay8.set(Calendar.YEAR, 1946);
        customer8.setBirthDate(birthDay8.getTime());
        customer8.setEmail("Eleanor@gmail.com");
        customer8.setLoyaltypoint(new Integer(0));
        em.persist(customer8);
        em.flush();
        //create Richard@gmail.com
        Users customer9 = new Users();
        customer9.setId(new Long(10009));
        customer9.setCountry(singapore);
        Calendar birthDay9 = Calendar.getInstance();
        birthDay9.set(Calendar.YEAR, 1966);
        customer9.setBirthDate(birthDay9.getTime());
        customer9.setEmail("Richard@gmail.com");
        customer9.setLoyaltypoint(new Integer(0));
        em.persist(customer9);
        em.flush();


        //creat outlet with outlet id 1 2 3 4

        Outlet o1 = new Outlet();
        o1.setId(new Long(10001));
        o1.setAddress(null);
        o1.setOutletName("NUS");
        o1.setAutoConfirmReservation(true);
        o1.setAddress("Kent Ridge Drive 1st");
        o1.setBookingWindow(new Integer(30));
        o1.setManagerEmails(" Jacob@sage.com ");
        o1.setNoShowPenalty(new Double(5));
        o1.setLeadTime_Day(new Integer(3));
        o1.setLatitude(new Double(1.297262476402216));
        o1.setLongitude(new Double(103.77152368351744));
        o1.setPhoneNumber("+6589323847");
        Calendar createTime1 = Calendar.getInstance();
        createTime1.set(2009, 10, 18, 0, 0, 0);
        o1.setCreateTime(createTime1);
        Query queryo1 = em.createQuery("SELECT c FROM FoodItem c WHERE c.outletId= :outletId");
        queryo1.setParameter("outletId", 10001);
        List<FoodItem> foodItems1 = queryo1.getResultList();
        o1.setFoodMenu(foodItems1);
        em.persist(o1);
        em.flush();


        Outlet o2 = new Outlet();
        o2.setId(new Long(10002));
        o2.setOutletName("Clementi");
        o2.setAutoConfirmReservation(false);
        o2.setAddress("Clementi Drive 1st");
        o2.setBookingWindow(new Integer(30));
        o2.setManagerEmails(" Peter@sage.com ");
        o2.setNoShowPenalty(new Double(5));
        o2.setLeadTime_Day(new Integer(3));
        o2.setLatitude(new Double(1.3103911717030983));
        o2.setLongitude(new Double(103.76645967289733));
        o2.setPhoneNumber("+6589323840");
        Calendar createTime2 = Calendar.getInstance();
        createTime2.set(2009, 10, 18, 0, 0, 0);
        o2.setCreateTime(createTime2);
        Query queryo2 = em.createQuery("SELECT c FROM FoodItem c WHERE c.outletId= :outletId");
        queryo2.setParameter("outletId", 10002);
        List<FoodItem> foodItems2 = queryo2.getResultList();
        o2.setFoodMenu(foodItems2);
        em.persist(o2);
        em.flush();

        Outlet o3 = new Outlet();
        o3.setId(new Long(10003));
        o3.setOutletName("Clementi");
        o3.setAutoConfirmReservation(true);
        o3.setAddress("Clementi Drive 1st");
        o3.setBookingWindow(new Integer(30));
        o3.setManagerEmails(" Edward@sage.com ");
        o3.setNoShowPenalty(new Double(5));
        o3.setLeadTime_Day(new Integer(3));
        o3.setLatitude(new Double(1.3103911717030983));
        o3.setLongitude(new Double(103.76645967289733));
        o3.setPhoneNumber("+6589309840");
        Calendar createTime3 = Calendar.getInstance();
        createTime3.set(2009, 10, 18, 0, 0, 0);
        o3.setCreateTime(createTime3);
        Query queryo3 = em.createQuery("SELECT c FROM FoodItem c WHERE c.outletId= :outletId");
        queryo3.setParameter("outletId", 10003);
        List<FoodItem> foodItems3 = queryo3.getResultList();
        o3.setFoodMenu(foodItems3);
        em.persist(o3);
        em.flush();

        Outlet o4 = new Outlet();
        o4.setId(new Long(10004));
        o4.setOutletName("JE");
        o4.setAutoConfirmReservation(true);
        o4.setAddress("Queen's Town Drive 1st");
        o4.setBookingWindow(new Integer(30));
        o4.setManagerEmails(" Tom@sage.com ");
        o4.setNoShowPenalty(new Double(10));
        o4.setLeadTime_Day(new Integer(7));
        o4.setLatitude(new Double(1.29151328769033));
        o4.setLongitude(new Double(103.80422517582701));
        o4.setPhoneNumber("+6589399840");
        Calendar createTime4 = Calendar.getInstance();
        createTime4.set(2009, 10, 18, 0, 0, 0);
        o4.setCreateTime(createTime4);
        Query queryo4 = em.createQuery("SELECT c FROM FoodItem c WHERE c.outletId= :outletId");
        queryo4.setParameter("outletId", 10004);
        List<FoodItem> foodItems4 = queryo4.getResultList();
        o4.setFoodMenu(foodItems4);
        em.persist(o4);
        em.flush();

        StakeholderFood s1 = new StakeholderFood();
        s1.setName("Crystal Jade");
        List<Outlet> outlets = new ArrayList<Outlet>();
        outlets.add(o1);
        outlets.add(o2);
        s1.setOutlet(outlets);
        s1.setPurchaseBI(true);
        Calendar createTime5 = Calendar.getInstance();
        createTime5.set(2009, 10, 1, 0, 0, 0);
        s1.setCreateTime(createTime5);
        s1.setAddress("Computing Drive 1st");
        s1.setIsActivated(true);
        s1.setPhoneNo("+6590302030");
        StakeholderTier tier1 = (StakeholderTier) em.find(StakeholderTier.class, "Gold");
        s1.setStakeholderTier(tier1);
        em.persist(s1);
        em.flush();

        StakeholderFood s2 = new StakeholderFood();
        s2.setName("Din Tai Fung");
        List<Outlet> outlets2 = new ArrayList<Outlet>();
        outlets2.add(o3);
        outlets2.add(o4);
        s2.setOutlet(outlets2);
        s2.setPurchaseBI(true);
        Calendar createTime6 = Calendar.getInstance();
        createTime6.set(2009, 10, 1, 0, 0, 0);
        s2.setCreateTime(createTime6);
        s2.setAddress("Engineering Drive 1st");
        s2.setIsActivated(true);
        s2.setPhoneNo("+6580302030");
        StakeholderTier tier2 = (StakeholderTier) em.find(StakeholderTier.class, "Silver");
        s2.setStakeholderTier(tier2);
        em.persist(s2);
        em.flush();

        Query q1 = em.createQuery("SELECT c FROM StakeholderFood c WHERE c.name=:stakeholderName");
        q1.setParameter("stakeholderName", "Crystal Jade");
        List<StakeholderFood> sfs1 = q1.getResultList();
        StakeholderFood sf1 = sfs1.get(0);
        Query q2 = em.createQuery("SELECT c FROM StakeholderFood c WHERE c.name=:stakeholderName");
        q2.setParameter("stakeholderName", "Din Tai Fung");
        List<StakeholderFood> sfs2 = q2.getResultList();
        StakeholderFood sf2 = sfs2.get(0);

        Outlet outlet1 = em.find(Outlet.class, new Long(10001));
        outlet1.setStakeholder(sf1);
        Outlet outlet2 = em.find(Outlet.class, new Long(10002));
        outlet2.setStakeholder(sf1);
        Outlet outlet3 = em.find(Outlet.class, new Long(10003));
        outlet3.setStakeholder(sf2);
        Outlet outlet4 = em.find(Outlet.class, new Long(10004));
        outlet4.setStakeholder(sf2);
        em.persist(outlet1);
        em.persist(outlet2);
        em.persist(outlet3);
        em.persist(outlet4);
        em.flush();


        Places placeCrystal = new Places();
        placeCrystal.setOutlet(o1);
        List<Reviews> reviews;
        Query q3 = em.createQuery("SELECT c FROM Reviews c");
        reviews = q3.getResultList();
        placeCrystal.setReviews(reviews);
        for (Reviews r : reviews) {
            r.setPlace(placeCrystal);
        }
        em.persist(placeCrystal);
    }

    public void updateAdData() {

        Query q1 = em.createQuery("SELECT c FROM StakeholderFood c WHERE c.name=:stakeholderName");
        q1.setParameter("stakeholderName", "Crystal Jade");
        List<StakeholderFood> sfs1 = q1.getResultList();
        StakeholderFood sf1 = sfs1.get(0);
        System.out.println("The first stkeholder we get hrer is" + sf1.getName());

        Query q2 = em.createQuery("SELECT c FROM StakeholderFood c WHERE c.name=:stakeholderName");
        q2.setParameter("stakeholderName", "Din Tai Fung");
        List<StakeholderFood> sfs2 = q2.getResultList();
        StakeholderFood sf2 = sfs2.get(0);
        System.out.println("The second stkeholder we get hrer is" + sf2.getName());

        Advertisement a1 = new Advertisement();
        a1.setId(new Long(1));
        a1.setStakeholder(sf1);
        a1.setIsActivated(true);

        Advertisement a2 = new Advertisement();
        a2.setId(new Long(2));
        a2.setStakeholder(sf1);
        a2.setIsActivated(true);

        Advertisement a3 = new Advertisement();
        a3.setId(new Long(3));
        a3.setStakeholder(sf2);
        a3.setIsActivated(true);

        Query q3 = em.createQuery("Select c from AdvertisementStatistics c");
        List<AdvertisementStatistics> adStats = q3.getResultList();
        a3.setAdvertisementStatistics(adStats);

        em.persist(a1);
        em.persist(a2);
        em.persist(a3);
        em.flush();

        List<Advertisement> adsOfSf1 = new ArrayList<Advertisement>();
        adsOfSf1.add(a1);
        adsOfSf1.add(a2);
        sf1.setAdvertisement(adsOfSf1);

        List<Advertisement> adsOfSf2 = new ArrayList<Advertisement>();
        adsOfSf2.add(a3);
        sf1.setAdvertisement(adsOfSf2);

        em.persist(sf1);
        em.persist(sf2);

    }
}